//
//  SportSave_ViewController.m
//  Bicycle
//
//  Created by dllo on 16/4/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SportSave_ViewController.h"
#import "CoreDataManager.h"
#import "BikeRecord.h"
@interface SportSave_ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *save_Distance;
@property (weak, nonatomic) IBOutlet UILabel *save_Time;
@property (weak, nonatomic) IBOutlet UILabel *save_Speed;
@property (weak, nonatomic) IBOutlet UITextField *save_dateTF;
@property (weak, nonatomic) IBOutlet UITextView *save_textView;

@end

@implementation SportSave_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    NSLog(@"path:%@",[[CoreDataManager shareInstance] applicationDocumentsDirectory] );
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view from its nib.
    
    
    
    //当前日期
    NSDate * senddate = [NSDate date];
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYYMMdd"];
    NSString *locationString = [dateformatter stringFromDate:senddate] ;
    
    
    
    
    NSLog(@"new%f",self.distance);
    
    self.save_dateTF.placeholder = locationString;
    self.save_Time.text = self.time;
    self.save_Distance.text = [NSString stringWithFormat:@"%.2f",self.distance];
    self.save_Speed.text = self.speed;

    
}
- (IBAction)Cancel:(id)sender {
    
    UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"确定删除?" message:nil preferredStyle:UIAlertControllerStyleAlert];
    [ac addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        //通知
        NSNotification *notice = [NSNotification notificationWithName:@"passBool" object:nil userInfo:@{@"start":@"0"}];
        [[NSNotificationCenter defaultCenter]postNotification:notice];
        
        
        
        //跳回根视图
        //让B的视图变为透明（由于父视图变为透明，任何B的子视图都会透明）。这样的效果将是C的视图从A的视图上滑出。
        self.presentingViewController.view.alpha = 0;
        //连续获取了两次presentingViewController
        [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
    }]];
    [self presentViewController:ac animated:YES completion:^{}];

    
}
- (IBAction)Finish:(id)sender {
    
    
    NSString *str = [NSString stringWithFormat:@"%f",self.distance];
    //通知
    NSNotification *noticeapp = [NSNotification notificationWithName:@"pass" object:nil userInfo:@{@"start":str}];
    [[NSNotificationCenter defaultCenter]postNotification:noticeapp];
    
    
    //    NSLog(@"%@",noticeapp);
    
    
    
    //跳回根视图
    //让B的视图变为透明（由于父视图变为透明，任何B的子视图都会透明）。这样的效果将是C的视图从A的视图上滑出。
    //    self.presentingViewController.view.alpha = 0;
    //    //连续获取了两次presentingViewController
    
    
    
    
    [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];

    //coredata
    NSEntityDescription *description = [NSEntityDescription entityForName:@"BikeRecord" inManagedObjectContext:[CoreDataManager shareInstance].managedObjectContext];
    //创建数据模型对象
    
    BikeRecord *record = [[BikeRecord alloc]initWithEntity:description insertIntoManagedObjectContext:[CoreDataManager shareInstance].managedObjectContext];
    //获取当前当地时间
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];

    NSLog(@"%@",localeDate);
    //存入coreData
    record.run_Date = localeDate;
    record.run_Text = self.save_textView.text;
    record.run_Time = self.save_Time.text;
    record.run_Distance = [NSNumber numberWithFloat:self.distance];
    record.run_AverSpeed = self.averSpeed;
    [[CoreDataManager shareInstance]saveContext];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
